package com.epam.concurrency.sincossummer;

/**
 * Runnable sin finder for the multi-threaded Sin Summer
 * @author Yuriy Miedviediev
 * @version 1.0 Build 8.05.2014
 */
public class Worker extends Thread {

    //bounds to find sin within
    private final double begin;
    private final double end;
    private static final double STEP = 0.0001;

    //variable to accumulate results
    private double result = 0.0;

    public Worker(double begin, double end) {
        this.begin = begin;
        this.end = end;
    }

    @Override
    public void run() {
        for(double num = this.begin; num <= this.end; num = SinCosRangeAccumulator.customRound(num + STEP)) {
            //round to 0.0001 to prevent big offsets
            result += SinCosRangeAccumulator.customRound(function(num));
        }
    }

    public double getResult() {
        return result;
    }

    public double function(double num) {
        return Math.sin(num) * Math.cos(num);
    }
}
